package com.vancouver.knows.ui.activity.citydiscount;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import butterknife.Bind;
import butterknife.OnClick;

import com.pangu.util.StringUtil;
import com.vancouver.knows.R;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.ui.activity.search.NewSearchActivity;
import com.vancouver.knows.ui.activity.search.SearchActivity;
import com.vancouver.knows.ui.base.LocationActivity;
import com.vancouver.knows.ui.common.MyAutoCompleteTextView;
import com.vancouver.knows.ui.fragment.citydiscount.CityDiscountResultPullControllerView;
import com.vancouver.knows.ui.fragment.citydiscount.ViewArea;
import com.vancouver.knows.ui.fragment.citydiscount.ViewFilter;
import com.vancouver.knows.ui.fragment.citydiscount.ViewProject;
import com.vancouver.knows.ui.fragment.citydiscount.ViewSort;
import com.vancouver.knows.ui.view.ExpandTabView;
import com.vancouver.knows.util.PhoConstants;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.DiscountCategoryInfo;
import com.vancouver.knows.business.task.mark.FetchCityDiscountsCategoryTaskMark;

/**
 * 城惠结果列表
 * 
 * @author cxlin
 * 
 */
public class CityDiscoutResultActivity extends LocationActivity {

	@Bind(R.id.expandTabView)
	ExpandTabView expandTabView;
	@Bind(R.id.discountResultControllerView)
	CityDiscountResultPullControllerView cityDiscountResultPullControllerView;
	private ViewArea viewArea;
	private ViewFilter viewFilter;
	private ViewProject viewProject;
	private ViewSort viewSort;
	private ArrayList<View> mViewArray = new ArrayList<View>();
	private BusinessModule bModule;
	private String city = "";
	private String category = "";
	private String subCategory = "";
	private String sort = "";
	private String filter = "";
	@Bind(R.id.auto_search)
	MyAutoCompleteTextView mAutoSearch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_citydiscount_result);

		Intent intent = getIntent();
		if (intent == null) {
			return;
		}

		int index = intent.getIntExtra("index", -1);
		if (index == -1) {
			return;
		}

		String filter = intent.getStringExtra("filter");
		if (StringUtil.isEmptyString(filter)) {
			filter = "";
		}

		category = intent.getStringExtra("category");
		if (category == null) {
			category = "";
		}

		subCategory = intent.getStringExtra("subcategory");
		if (subCategory == null) {
			subCategory = "";
		}
		bModule = imContext.getBusinessModule();
		viewArea = new ViewArea(this);
		viewFilter = new ViewFilter(this, filter);
		viewProject = new ViewProject(this, bModule.getCacheManager().getDiscountCategoryInfoCache(), index, subCategory);
		viewSort = new ViewSort(this);
		mViewArray.add(viewArea);
		mViewArray.add(viewProject);
		mViewArray.add(viewSort);
		mViewArray.add(viewFilter);
		ArrayList<String> mTextArray = new ArrayList<String>();
		mTextArray.add(getResources().getString(R.string.whole_area));
		mTextArray.add(getResources().getString(R.string.restaurant));
		mTextArray.add(getResources().getString(R.string.default_sort));
		mTextArray.add(getResources().getString(R.string.show_all));
		expandTabView.setValue(mTextArray, mViewArray);
		expandTabView.setTitle(viewArea.getShowText(), 0);
		expandTabView.setTitle(viewProject.getShowText(), 1);
		expandTabView.setTitle(viewSort.getShowText(), 2);
		expandTabView.setTitle(viewFilter.getShowText(), 3);

		initListener();

		DiscountCategoryInfo discountCategoryInfo = bModule.getCacheManager().getDiscountCategoryInfoCache().get(index);
		category = discountCategoryInfo.getName();
		FetchCityDiscountsCategoryTaskMark aTaskMark = bModule.getTaskMarkPool().getFetchCityDiscountsCategoryTaskMark(
				category, subCategory, city, filter, sort);
		cityDiscountResultPullControllerView.loadData(aTaskMark);

		mAutoSearch.setHint(getResources().getString(R.string.search_discount_default));
	}

	@OnClick(R.id.auto_search)
	public void onAutoSearchClick() {
		Intent searchIntent = new Intent(this, NewSearchActivity.class);
		startActivity(searchIntent);
	}

	private void initListener() {

		viewArea.setOnSelectListener(new ViewArea.OnSelectListener() {

			@Override
			public void getValue(String distance, String showText) {
				city = showText.equals("全部区域") ? "" : showText;
				onRefresh(viewArea, showText);
			}
		});

		viewProject.setOnSelectListener(new ViewProject.OnSelectListener() {

			@Override
			public void getValue(String showText, String category, String subCategory) {
				CityDiscoutResultActivity.this.category = category;
				CityDiscoutResultActivity.this.subCategory = subCategory.equals(getResources().getString(R.string.all)) ? ""
						: subCategory;
				onRefresh(viewProject, showText);
			}
		});

		viewSort.setOnSelectListener(new ViewSort.OnSelectListener() {

			@Override
			public void getValue(String distance, String showText) {
				sort = showText.equals("默认排序") ? "" : showText;
				onRefresh(viewSort, showText);
			}
		});

		viewFilter.setOnSelectListener(new ViewFilter.OnSelectListener() {

			@Override
			public void getValue(String distance, String showText) {
				filter = showText.equals("显示全部") ? "" : showText;
				onRefresh(viewFilter, showText);
			}
		});
	}

	private void onRefresh(View view, String showText) {
		expandTabView.onPressBack();
		int position = getPositon(view);
		if (position >= 0 && !expandTabView.getTitle(position).equals(showText)) {
			expandTabView.setTitle(showText, position);
			FetchCityDiscountsCategoryTaskMark aTaskMark = bModule.getTaskMarkPool()
					.getFetchCityDiscountsCategoryTaskMark(category, subCategory, city, filter, sort);
			cityDiscountResultPullControllerView.loadData(aTaskMark);
		}
	}

	private int getPositon(View tView) {
		for (int i = 0; i < mViewArray.size(); i++) {
			if (mViewArray.get(i) == tView) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public void subHandleMessage(Message msg) {
		switch (msg.what) {
			case Constants.BUSINESS_STATUS_CITYDISCOUNT_COMMENT_COUNT_CHANGE:
				cityDiscountResultPullControllerView.flush();
				break;
		}
	}

	@Override
	protected boolean isUpdateLocation() {
		return true;
	}
}
